//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace DNAide.Core
{

	/// <summary>
	/// Validation error class.
	/// </summary>
	public class ValidationError
	{
		private string _fieldName;
		private string _friendlyFieldName;
		private int _errorNumber;
		private string _errorMessage;

		/// <summary>
		/// Field name
		/// </summary>
		public String FieldName
		{
			get { return _fieldName; }
		}

		/// <summary>
		/// Friendly field name
		/// </summary>
		public String FriendlyFieldName
		{
			get 
			{
				if (String.IsNullOrEmpty(_friendlyFieldName))
					return _fieldName;
				return _friendlyFieldName; 
			}
		}

		/// <summary>
		/// Error number
		/// </summary>
		public Int32 ErrorNumber
		{
			get { return _errorNumber; }
		}

		/// <summary>
		/// Error message
		/// </summary>
		public String ErrorMessage
		{
			get { return _errorMessage; }
		}

		/// <summary>
		/// Validation error
		/// </summary>
		/// <param name="fieldName">fieldName</param>
		/// <param name="friendlyFieldName">friendlyFieldName</param>
		/// <param name="errorNumber">errorNumber</param>
		/// <param name="errorMessage">errorMessage</param>
		public ValidationError(string fieldName, string friendlyFieldName, int errorNumber, string errorMessage)
		{
			_fieldName = fieldName;
			_friendlyFieldName = friendlyFieldName;
			_errorNumber = errorNumber;
			_errorMessage = errorMessage;
		}
	}


	/// <summary>
	/// Validation exception - includes a List of validation errors
	/// </summary>
	public sealed class ValidationException : Exception
	{
		private List<ValidationError> _validationErrors = new List<ValidationError>();

		/// <summary>
		/// Validation errors
		/// </summary>
		public List<ValidationError> ValidationErrors
		{
			get
			{
				return _validationErrors;
			}
		}

		/// <summary>
		/// Message - Will concat and return all validation error messages into a string
		/// </summary>
		public override String Message
		{
			get
			{
				if (_validationErrors.Count > 0)
				{
					string retval = string.Empty;
					foreach (ValidationError error in _validationErrors)
					{
						retval = StringAide.FormatInvariant("{0}: ({1}) {2}{3}",
							error.FieldName, error.ErrorNumber, error.ErrorMessage, Environment.NewLine);
					}
					return retval;
				}
				else
				{
					return base.Message;
				}
			}
		}


		/// <summary>
		/// Validation exception
		/// </summary>
		public ValidationException()
		{
		}

		/// <summary>
		/// Validation exception
		/// </summary>
		/// <param name="errors">errors</param>
		public ValidationException(List<ValidationError> errors)
		{
			_validationErrors = errors;
		}

	}

}
